package com.controller;

import com.annotation.Logging;
import com.bean.CalanderData;
import com.bean.CommentContent;

import com.bean.Users;
import com.bean.vo.CalenderVo;
import com.service.CalanderDataService;
import com.service.CommentContentService;
import com.utils.AjaxResult;
import com.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/hanCalender")
public class CalendarController {
    private Logger logger= LoggerFactory.getLogger(CalendarController.class);

    @Autowired
    private CalanderDataService calanderDataService;

    @Autowired
    private CommentContentService commentContentService;

    //查看汉族日历
    @RequestMapping("/checkDay")
    @Logging
    public AjaxResult checkDay(@RequestBody CalanderData calanderData,HttpServletRequest request) {

         if(StringUtils.isEmpty(calanderData.getTruedate()) ){
            calanderData.setTruedate(StringUtils.getDays());//暂时就这么设置
         }
            List<CalanderData> calanderDataList= calanderDataService.selectCalanderData(calanderData);

        if(calanderDataList.size()<=0){
            return AjaxResult.error("未查到数据");
        }else{
            CalanderData calanderData1=  calanderDataList.get(0);
            CalenderVo calenderVo=new CalenderVo();
            BeanUtils.copyProperties(calanderData1,calenderVo);

            List<CommentContent>commentContentList= commentContentService.selectCommentContent(calanderData1.getId().toString());
            calenderVo.setCommentList(commentContentList);
            return AjaxResult.success(calenderVo);
        }


    }


    //添加评论
    @RequestMapping("/addComment")
    @Logging
    public AjaxResult addComment(@RequestBody CommentContent commentContent,HttpServletRequest request) throws Exception {
       int i= commentContentService.addToComment(commentContent);
       if(i!=1){
           throw new Exception();//添加失败直接抛异常
       }
        return AjaxResult.success();
    }





}
